import flash.text.TextFormat;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.display.Sprite;
import flash.display.Shape;

import nme.Assets;
class RotationText extends Sprite
{  
  private var _format:TextFormat;
  private var _textField:TextField;
  private var _textContainer:Sprite;
  
  public function new(?textContent:String = "", ?fontSize:Int = 12, ?color:Int = 0x000000)
  {
    //Optionally, register the embedded font with the global font list
    //This allows the font to be used by any SWF files running in the Flash Player
    var font = Assets.getFont ("assets/VeraSe.ttf");
    super();
    //1. Create a text format object
    _format = new TextFormat();
    _format.size = fontSize;
    _format.color = color;
    
    //The name of the font should match 
    //the "name" parameter in the embed tag
    _format.font = font.fontName;
    
    //2. Create a TextField object
    _textField = new TextField();
    _textField.embedFonts = true;
    _textField.autoSize = TextFieldAutoSize.CENTER;
    _textField.text = textContent;
    _textField.setTextFormat(_format);
    _textField.antiAliasType = flash.text.AntiAliasType.ADVANCED;
    
    //3. Create a sprite object and add the _textContainer to it
    _textContainer = new Sprite();
    addChild(_textContainer);
    _textContainer.addChild(_textField);
    
    //Position the container sprite so that the text's baseline is aligned with the vector
    _textContainer.y = 0 - Std.int(_format.size);
  }
  
  //Getters and setters
  //text
  public var text(get_text,set_text):String;
  public function get_text():String
  {
    return _textField.text;
  }
  public function set_text(text:String):String
  {
    _textField.text = text;
    _textField.setTextFormat(_format);
    return _textField.text;
  }
  
  //fontSize
  public var fontSize(get_fontSize,set_fontSize):Int;
  public function get_fontSize():Int
  {
    return Std.int(_format.size);
  }
  public function set_fontSize(fontSize:Int):Int
  {
    _format.size = fontSize;
    //the TextFormat needs to be reapplied for size change to take effect
    _textField.setTextFormat(_format);
    
    //Reposition the text's container sprite so that the baseline remains the same
    _textContainer.y = 0 - Std.int(_format.size);
    return fontSize;
  }
  
  //color
  public var color(get_color,set_color):Int;
  public function get_color():Int
  {
    return Std.int(_format.color);
  }
  
  public function set_color(color:Int):Int
  {
    _format.color = color;
    _textField.setTextFormat(_format);
    return Std.int(_format.color);
  }
}